package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author sunjipu
 * @create 2020-04-07 9:52
 */
@Api(tags = "商品SKU接口")
@RestController
@RequestMapping("admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 根据spuId 查询spuImageList
     * @param spuId
     * @return
     */
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> getSpuImageList(@PathVariable("spuId") Long spuId) {
        List<SpuImage> spuImageList = manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    @GetMapping("spuSaleAttrList/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrList(@PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> spuSaleAttrList = spuInfoService.getSpuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        // 调用服务层
        spuInfoService.saveSkuInfo(skuInfo);
        return Result.ok();
    }
    @GetMapping("/list/{page}/{limit}")
    public  Result<IPage<SkuInfo>> listSkuPage(@PathVariable("page") Long page,
                              @PathVariable("limit") Long limit,
                              SkuInfo skuInfo) {
        // 调用服务层
        Page<SkuInfo> pageParam = new Page<>(page, limit);
        IPage<SkuInfo> pageModel = spuInfoService.selectPage(pageParam, skuInfo);
        return Result.ok(pageModel);
    }
    //上架
    @GetMapping("/onSale/{skuId}")
    public Result shangJia(@PathVariable("skuId")Long skuId){
        spuInfoService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result xiaJia(@PathVariable("skuId")Long skuId){
        spuInfoService.cancelSale(skuId);
        return Result.ok();
    }
}